<template>
  <!-- 应收账单 -->
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="exportBill"
          v-auth-button="['export/receivableBill']"
          >导出</el-button
        >
      </div>
      <div class="right-panel">
        <div class="right-panel-search">
          <sc-table-select
            style="width: 100%"
            v-model="searchSelectCustomer"
            :apiObj="apiObjCustomer"
            :table-width="700"
            clearable
            collapse-tags
            collapse-tags-tooltip
            :props="props"
            :params="customerForm"
            @change="changeCustomer"
          >
            <template #header="{ form, submit }">
              <el-form :model="customerForm">
                <el-row :gutter="20">
                  <el-col :span="8">
                    <el-input
                      v-model="customerForm.name"
                      placeholder="客户名称"
                      clearable
                      style="width: 100%"
                    ></el-input>
                  </el-col>
                  <el-col :span="6">
                    <el-date-picker
                      v-model="customerForm.startTime"
                      value-format="YYYY-MM-DD"
                      type="date"
                      placeholder="开始时间"
                      :teleported="false"
                      style="width: 100%"
                    ></el-date-picker>
                  </el-col>
                  <el-col :span="6">
                    <el-date-picker
                      v-model="customerForm.endTime"
                      value-format="YYYY-MM-DD"
                      type="date"
                      placeholder="结束时间"
                      :teleported="false"
                      style="width: 100%"
                    ></el-date-picker>
                  </el-col>
                  <el-col :span="4">
                    <el-form-item width="100%">
                      <el-button type="primary" @click="submit">查询</el-button>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
              <!-- <el-alert title="自定义FORM插糟 传递了form对象和提交方法" type="info" /> -->
            </template>
            <el-table-column
              prop="customerName"
              label="客户名称"
              min-width="150"
            ></el-table-column>
            <el-table-column
              prop="numbers"
              label="合同成交量"
              min-width="150"
            ></el-table-column>
          </sc-table-select>
          <el-select
            v-if="userList.length != 0"
            v-model="searchValue.adminId"
            placeholder="请选择业务员"
            style="width: 100%"
            clearable
            filterable
          >
            <el-option
              v-for="item in userList"
              :key="item.id"
              :label="item.username"
              :value="item.id"
            />
          </el-select>
          <el-date-picker
            v-model="searchValue.startTime"
            type="date"
            placeholder="选择开始日期"
            value-format="YYYY-MM-DD"
            style="width: 100%"
          >
          </el-date-picker>
          <el-date-picker
            v-model="searchValue.endTime"
            type="date"
            placeholder="选择结束日期"
            value-format="YYYY-MM-DD"
            style="width: 100%"
          >
          </el-date-picker>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="upsearch"
          ></el-button>
        </div>
      </div>
    </el-header>
    <div class="count">
      <p>应收总额：{{ countMoney.contractMoney ?? 0 }}元</p>
      <p>实收总额：{{ countMoney.actualMoney ?? 0 }}元</p>
    </div>
    <el-main class="nopadding">
      <scTable
        ref="table"
        :apiObj="apiObj"
        :params="params"
        row-key="id"
        stripe
        @row-dblclick="handleRowDblclick"
        :showSummary="true"
        :summary-method="getSummaries"
      >
        <!-- <el-table-column label="ID" prop="id" width="50"></el-table-column> -->
        <el-table-column
          label="客户"
          prop="customerName"
          min-width="150"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column label="合同编号" prop="num" min-width="150">
        </el-table-column>

        <el-table-column label="签订日期" prop="orderTime" min-width="150">
        </el-table-column>
        <el-table-column label="应收金额" prop="actualMoney" min-width="150">
        </el-table-column>
        <el-table-column
          label="实收金额"
          prop="customerPaymentMoney"
          min-width="150"
        >
        </el-table-column>
        <el-table-column
          label="业务员"
          prop="customerAdminName"
          min-width="150"
        >
        </el-table-column>
        <!-- <el-table-column label="收款状态" width="250">
          <template #default="scope">
            <p v-if="scope.row.isCollection == 0" style="color: #f56c6c">
              未完成
            </p>
            <p v-if="scope.row.isCollection == 1" style="color: #2bd814">
              已完成
            </p>
          </template>
        </el-table-column> -->
        <el-table-column label="操作" fixed="right" align="center" width="150">
          <template #default="scope">
            <el-button-group>
              <el-button
                text
                type="primary"
                size="small"
                @click="table_show(scope.row, scope.$index)"
                >查看合同</el-button
              >
            </el-button-group>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>
</template>

<script>
import { exportFile } from "@/utils/exportFile"
export default {
  name: "customerBill",
  data () {
    return {
      apiObj: this.$API.reportForm.getCustomerBillList,
      params: {
        isCollection: 0
      },
      searchValue: {
        adminId: '',//业务员id
        customerId: '',//客户id
        startTime: "",
        endTime: "",
      },
      apiObjCustomer: this.$API.finance.plancollect.getCustomerNumbers,
      customerForm: {
        name: null,
        startTime: null,
        endTime: null,
      },
      props: {
        label: "customerName",
        value: "id",
      },
      searchSelectCustomer: {},
      columnsFilters: ["money"],
      countMoney: {},
      userList: [],
      userInfo: {},
    }
  },
  mounted () {
    this.userInfo = this.$TOOL.data.get("USER_INFO")
    this.getUserList()
    this.getCountMoney()
  },
  methods: {
    async getUserList () {
      let res = await this.$API.user.getUserList.get({ page: 1, limit: 1000 })
      this.userList = res.data
    },
    //统计
    async getCountMoney () {
      let { data } = await this.$API.reportForm.getCustomerTotalMoney.get(this.searchValue)
      this.countMoney = data

    },
    //导出
    async exportBill () {
      if (!this.searchValue.customerId) {
        this.$message.error('请选择客户')
        return
      }
      let res = await this.$API.reportForm.exportReceivableBill.get(this.searchValue)
      exportFile(res, '应收账单.xls')
    },
    //合计行统计
    getSummaries (param) {
      console.log(param)
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "页合计"
          return
        }
        if (this.columnsFilters.includes(column.property)) {
          const values = data.map((item) =>
            Number(item[column.property])
          )
          if (!values.every((value) => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            sums[index] += " 元"
          } else {
            sums[index] = ""
          }
        }
      })
      return sums
    },

    changeCustomer (val) {
      this.searchValue.customerId = val.id
    },

    // 查看合同详情
    table_show (row) {
      this.$router.push({
        // path: '/contract/contractManagement/contractDetail',
        path: "/contract/contractManagement/detailList",
        query: {
          id: row.id,
        },
      })
    },
    //搜索
    upsearch () {
      this.getCountMoney()
      this.$refs.table.upData(this.searchValue)
    },

    //本地更新数据
    handleSaveSuccess () {
      this.$refs.table.refresh()
    },

    // 双击表格
    handleRowDblclick (row) {
      this.table_show(row)
    },
  },
};
</script>

<style lang="scss" scoped>
.count {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  height: 50px;
  padding-left: 12px;
  font-size: 14px;
  color: #555;
  font-weight: 600;
  p {
    margin-right: 20px;
  }
}
</style>
